package cameratricks;

import hypermedia.video.OpenCV;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Frame;
import java.util.Date;

import processing.core.PApplet;
import processing.core.PImage;
import processing.video.Capture;


public class TrickCarousel extends PApplet implements UsesCamera {
	Capture capture;
	
	int i = 0;
	Renderer[] renderers;
	OpenCV opencv;
	private Renderer current_renderer;

	private Date last_switch_time;
	
	static public void main(String args[]) {
		  PApplet.main(new String[] { "--present", "FullScreenApp" });
	}

	public void setup() {
		size(screen.width, screen.height);
		
//		capture = new Capture(this, width, height, 30);
		
		renderers = new Renderer[7];
		
		renderers[2] =  new CameraRectangleizer(this);
		renderers[1] = new CameraCircleizer(this);
		renderers[0] = new Otsuizer(this);
		renderers[3] = new FaceWack(this);
		renderers[4] = new Posterizer(this);
		renderers[5] = new BlobConnector(this);
		renderers[6] = new DialogBubbles(this);
		
		opencv = new OpenCV(this);
		opencv.capture(width, height);

	    opencv.cascade( OpenCV.CASCADE_FRONTALFACE_ALT );    // load the FRONTALFACE description file
		current_renderer = renderers[renderers.length-1];
		
//		capture.stop();
		
		last_switch_time = new Date();
	}
	
	public void draw() {
		opencv.read();
		
	
		Date now = new Date();
		int TIMESLICE = 60;
		if (i == 6)
			TIMESLICE = 240;
		else TIMESLICE = 60;
//		
//		if (now.getTime() - last_switch_time.getTime() > TIMESLICE * 1000) {
//			switchRenderer();
//			last_switch_time = now;
//		}
		
		current_renderer.draw(opencv);
	}
	
	public Capture getCapture() {
		return capture;
	}

	public void captureEvent(Capture myCapture) {
		
		myCapture.read();
	}
	
	public void switchRenderer()
	{
		System.out.println("Switching from " + i);
		i = (++i) % renderers.length;
		current_renderer = renderers[i];
	}
	
	  public void keyPressed() {
		  if (key == 'x') {
			  switchRenderer();
		  }
	  }
}
